//引入模块
import Api from './api.js'
import Cookie from './cookie.js'
// import $$$ from '../jquery.cookie.js'

new Vue({
    el:'#app',
    data(){
        return{
            userid:'',
            passwd:'',

            screech:'',

            flag:''
        }
    },
    mounted(){
        this.useiZh()
        this.weilogin()
    },
    methods:{
        //记住账户
        useiZh(){
            this.userid = Cookie.getCookie("zh")

        },
        async weilogin(){
            //判断是否微博登录
            let a = document.cookie
            let flag = sessionStorage.getItem("flagLogin")

            if(a!=""&&flag!=1)
            {
                console.log(a)

                let f = 0;

                let info = a.split(";")
                let mes = info[info.length-1]
                let i = mes.replace("winfo=",'')

                let aaa = JSON.parse(unescape(decodeURI(i)))

                let allinfo = await Api.login()

                for(let i=0;i<allinfo.length;i++){

                    if(aaa.zh == allinfo[i].zh){

                        f=1
                        sessionStorage.setItem("userinfo",JSON.stringify(allinfo[i]))
                        sessionStorage.setItem("zh",aaa.zh)

                        //抑制cookie标记
                        sessionStorage.setItem("flagLogin","1")

                        window.location.href='./mem.html'

                    }
                }
                if(f==0){
                    alert("进行注册")
                    await Api.sign(aaa.zh,aaa.nc,"123456")
                    await Api.upTx(aaa.tx,aaa.zh)
                    sessionStorage.setItem("zh",aaa.zh)
                    this.$alert('<span>账号：'+aaa.zh+'</span><br><span>昵称：'+aaa.nc+'</span><br><span>密码：123456'+'</span><br>', '账号信息', {
                        dangerouslyUseHTMLString: true
                    }).then(action => {

                        //抑制cookie标记
                        sessionStorage.setItem("flagLogin","1")

                        window.location.href='./mem.html'
                    });
                }


            }



        },
        //登录功能
        async login(){
            //获取所有用户信息
            let userinfo =  await Api.login()

            //判断是否为邮箱
            let regExp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if (!regExp.test(this.userid)){
                this.$message({
                    showClose: true,
                    message: '您的邮箱输入不规范！',
                    type: 'error'
                });
            }else {
                //判断密码是否为字符数字
                regExp = /^[A-Za-z0-9]{6,14}$/
                if(!regExp.test(this.passwd)){
                    this.$message({
                        showClose: true,
                        message: '您的密码输入不规范！',
                        type: 'error'
                    });
                }else {
                    for(let i=0;i<userinfo.length;i++){
                        if(this.userid==userinfo[i].zh&&this.passwd==userinfo[i].pwd){
                            //账号正确时，跳转并将当前用户id存入cookie

                            if(this.flag==true){
                                Cookie.setCookie("zh",this.userid,'s10800')
                            }

                            sessionStorage.setItem("userinfo",JSON.stringify(userinfo[i]))

                            sessionStorage.setItem("zh",userinfo[i].zh)

                            window.location.href='./mem.html'
                        }
                    }
                    this.$message({
                        showClose: true,
                        message: '账号或密码错！',
                        type: 'error'
                    });
                }
            }

        },



    }
})


